<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com>
// +----------------------------------------------------------------------

namespace app\duty\controller;

use app\common\controller\Admin;
use app\admin\controller\ajax\Library as Ajax;
use app\basic\controller\api\Route;

/**
 * 全景态势
 * @package app\plan
 */
class Panorama  extends Admin

{

    protected $api;
    public function _initialize()
    {
        parent::_initialize();
        $this->ajax= new Ajax();
    }
    /**
     * 首页
     *
     */
    public function index() {
        $model=model('events');
        //事件id
        $id=$model->where(array('eventStatus'=>1))->order('id desc')->limit(1)->value('id');

        if($id){

         //执行id
            $rescueExecId=model('Plan/record')->where(array('eventId'=>$id))->value('id');
            if(IS_AJAX){
                $recorddetail=collection(model('Plan/record')->with('records')->where(array('eventId'=>$id))->select())->toArray();

                if($recorddetail){
                    $count=count($recorddetail);
                    $recorddetaillist=$recorddetail[0]['records'];
                    return json(['code'=>1,'data'=>$recorddetaillist,'count'=>$count]);
                }else{
                    return json(['code'=>0,'msg'=>'暂无预案']);
                }
            }
            $this->assign('id',$id);
            $euipe=collection(model('basic/equipe')->with('enterprise')->where(array('categoryId'=>2))->select());
            $this->assign('euipe',$euipe);
            //救援物资类别
            $type=$this->ajax->getEquipeType()->getData();
            $this->assign('type',$type);
            //危险源坐标
            $position=$model->where(array('eventStatus'=>1))->order('id desc')->limit(1)->with('hazard')->select();
            $position=$position[0]['hazard']['latitudeLongitude'];
            //事发企业id
            $enterpriseId=$model->where(array('eventStatus'=>1))->order('id desc')->limit(1)->value('enterpriseId');
            $enterprise = model('basic/enterprise')->find($enterpriseId);
            //逃跑路径
            $routes = json_encode($enterprise->routes);
            $this->assign('routes',$routes);
            //危险源id
            $dangerSourceId=$model->where(array('eventStatus'=>1))->order('id desc')->limit(1)->value('dangerSourceId');
            $this->assign('enterprise',$enterpriseId);
            $this->assign('dangersource',$dangerSourceId);
            $this->assign('position',$position);
            $this->assign('rescueExecId',$rescueExecId);
            return $this->fetch();
          }{

            $this->assign('id',null);
            $this->assign('routes',null);
            return $this->fetch();
        }


         }


    /**
     * 演练执行明细
     *
     */
    public function  addActionStep($actionstepId=null){
        if (empty($actionstepId)) {
            return json(['code'=>0,'msg'=>'缺少参数,请检查']);
        }

        $res=model('plan/recorddetail')->where(array('id'=>$actionstepId))->update(['actionStatus'=>1,'implementStatus'=>1,'implementTime'=>time()]);

        if ($res) {
            return json(['code'=>1,'msg'=>'正在执行中']);
        }else{
            return json(['code'=>0,'msg'=>'执行失败,请检查']);
        }
    }

}



